import com.alibaba.fastjson.JSON;
import com.fasteat.common.entity.Result;
import com.fasteat.menu.FastEatMenuApplication;
import com.fasteat.menu.entity.Material;
import com.fasteat.menu.entity.Menu;
import com.fasteat.menu.mapper.MenuMapper;
import com.fasteat.menu.service.MenuService;
import com.fasteat.menu.service.client.SearchClient;
import com.fasteat.menu.service.client.UserClient;
import com.fasteat.menu.service.impl.MenuServiceImpl;
import com.fasteat.menu.util.UpdateProxy;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author run
 * @since 2021/1/23 11:40
 */
//@SpringBootTest(classes = FastEatMenuApplication.class)
//@RunWith(SpringRunner.class)
public class ContentTest {

    @Autowired
    private SearchClient searchClient;

    @Autowired
    private UserClient userClient;

    @Autowired
    private MenuService menuService;

    @Autowired
    private UpdateProxy updateProxy;

    @Autowired
    private MenuMapper menuMapper;


    @Test
    public void run(){
            for (int i = 1; i < 100; i++) {
                try {
                    String menuUrl = "https://www.xiachufang.com/category/40076/?page=" + i;
                    Connection connection = Jsoup.connect(menuUrl);
                    Document document = connection.get();
                    Element body = document.body();
                    Element first = body.getElementsByClass("normal-recipe-list").first();
                    Element ul = first.getElementsByTag("ul").get(0);
                    Elements lis = ul.getElementsByTag("li");
                    for (Element li : lis) {
                        try {
                            String url = li.getElementsByClass("info pure-u")
                                    .first()
                                    .getElementsByTag("p")
                                    .get(0)
                                    .getElementsByTag("a")
                                    .get(0)
                                    .attr("href");
                            url = "https://www.xiachufang.com" + url;
                            Element article = Jsoup.connect(url).get().body();
                            String cover = article.getElementsByClass("cover image expandable block-negative-margin")
                                    .first()
                                    .getElementsByTag("img")
                                    .first()
                                    .attr("src");
                            String title = article.getElementsByClass("page-title")
                                    .first()
                                    .text();
                            Elements materialEles = article.getElementsByClass("ings")
                                    .first()
                                    .getElementsByTag("tr");
                            List<Material> materials = new ArrayList<>();
                            materialEles.forEach(element -> {
                                Element elementByTag = element.getElementsByClass("name")
                                        .first();
                                String materialName = elementByTag.getElementsByTag("a").isEmpty()
                                        ? elementByTag.text()
                                        : elementByTag.getElementsByTag("a")
                                        .first()
                                        .text();
                                if (containsEmoji(materialName)) {
                                    materialName = "emoji";
                                }
                                materialName = materialName.length() > 50 ? materialName.substring(0, 49) : materialName;
                                String dosage = element.getElementsByClass("unit")
                                        .first()
                                        .text();
                                if (containsEmoji(dosage)) {
                                    dosage = "emoji";
                                }
                                dosage = dosage.length() > 10 ? dosage.substring(0, 9) : dosage;
                                Material material = new Material();
                                material.setMaterialName(materialName);
                                material.setDosage(dosage);
                                materials.add(material);
                            });

                            Menu menu = new Menu();
                            menu.setAuthorId("1354333036514115586");
                            menu.setMenuCover(cover);
                            menu.setMenuContext("");
                            menu.setMenuTitle(title);
                            menu.setMaterials(materials);
                            menu.setMenuTag(materials.get(0).getMaterialName());
                            if (!containsEmoji(title)) {
                                menuService.createMenu(menu);
                            }
                        } catch (Exception e) {
                            continue;
                        }
                    }
                } catch (Exception e) {
                    continue;
                }
            }
    }


    public static boolean containsEmoji(String source) {
        int len = source.length();
        boolean isEmoji = false;
        for (int i = 0; i < len; i++) {
            char hs = source.charAt(i);
            if (0xd800 <= hs && hs <= 0xdbff) {
                if (source.length() > 1) {
                    char ls = source.charAt(i+1);
                    int uc = ((hs - 0xd800) * 0x400) + (ls - 0xdc00) + 0x10000;
                    if (0x1d000 <= uc && uc <= 0x1f77f) {
                        return true;
                    }
                }
            } else {
                // non surrogate
                if (0x2100 <= hs && hs <= 0x27ff && hs != 0x263b) {
                    return true;
                } else if (0x2B05 <= hs && hs <= 0x2b07) {
                    return true;
                } else if (0x2934 <= hs && hs <= 0x2935) {
                    return true;
                } else if (0x3297 <= hs && hs <= 0x3299) {
                    return true;
                } else if (hs == 0xa9 || hs == 0xae || hs == 0x303d || hs == 0x3030 || hs == 0x2b55 || hs == 0x2b1c || hs == 0x2b1b || hs == 0x2b50|| hs == 0x231a ) {
                    return true;
                }
                if (!isEmoji && source.length() > 1 && i < source.length() -1) {
                    char ls = source.charAt(i+1);
                    if (ls == 0x20e3) {
                        return true;
                    }
                }
            }
        }
        return  isEmoji;
    }

    @Test
    public void test(){
        List<String> ids = new ArrayList<>();
        ids.add("1363751108048150529");
        ids.add("1363751237190770689");
        menuMapper.getSimpleMenuVosOfIds(ids);
    }
}
